package com.saint.lt.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.saint.lt.domain.Project;
import com.saint.lt.domain.Team;

@Repository
public class ProjectDAOImpl implements ProjectDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void add(Project project) {
		sessionFactory.getCurrentSession().save(project);
	}

	@Override
	public void remove(Project project) {
		if (null != project) {
			sessionFactory.getCurrentSession().delete(project);
		}

	}

	@Override
	public void remove(int id) {
		Project project = (Project) sessionFactory.getCurrentSession().load(Project.class,
				id);
		remove(project);
	}

	@Override
	public List<Project> list() {
		List<Project> projects = sessionFactory.getCurrentSession().createCriteria(Project.class).list();
		return projects;
	}

	@Override
	public Project get(int id) {
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(Project.class);
		cr.add(Restrictions.eq("id", id));
		Project result = (Project) cr.uniqueResult();
		return result;
	}

}
